<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jedrzej\Pimpable\PimpableTrait;

class IntegralProduct extends Model
{
    use HasFactory;
    use PimpableTrait;
    use SoftDeletes;

    /**
     *不允许批量赋值的字段
     */
    public $guarded  = ['attr_v'];

    /**
     * 强制转换类型
     */
    protected $casts = ['rotation_chart' => 'json'];

    /**
     * 可搜索的字段
     */
    public $searchable = [
        'id',
        'title',
        'shel_status',
        'created_at'
    ];

    /**
     * 设置模糊搜索
     */
    public $searchableModels = [
        'title' => '%field%',
    ];

    /**
     *可排序字段
     */
    public $sortable = [
        'created_at',
        'id',
        'status',
        'sales',
        'shel_status'
    ];

    /**
     * 默认倒序
     */
    protected $defaultSortCriteria = ['id,desc'];


    /**
     * 关联商品模型
     */

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'product_id');
    }

    /**
     * 关联商户模型
     */

    public function merchantRegister()
    {
        return $this->belongsTo(MerchantRegister::class, 'merchant_id', 'id');
    }

    /**
     * 关联单规格的模型
     */

    public function attrResult()
    {
        return $this->belongsTo(ProductAttrResult::class,'product_id','product_id');
    }

}
